package meituan;

import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.PriorityQueue;

public class Problem3plus {
    public static void main(String[] args) throws IOException {
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
        int T = Integer.parseInt(reader.readLine());
        for (int i0 = 0; i0 < T; i0++) {
            int N = Integer.parseInt(reader.readLine());//几桌
            char[] a = reader.readLine().toCharArray();//每桌的人数
            int M = Integer.parseInt(reader.readLine());//几人
            char[] b = reader.readLine().toCharArray();//每个人的性别
            //创建一个对象为优先队列的集合，集合0是0人桌队列，集合1是1人桌队列
            //优先队列数据结构为二叉堆(默认小顶堆，大顶堆需要添加逆序比较器Collections.reverseOrder)
            List<PriorityQueue<Integer>> queueList = new ArrayList<>();
            queueList.add(new PriorityQueue<>());
            queueList.add(new PriorityQueue<>());
            for (int i = 0; i < N; i++) {
                if (a[i] - '0' < 2) {
                    //不同人桌的队列里放从小到大的桌号
                    queueList.get(a[i] - '0').offer(i + 1);
                }
            }
            int x;
            for (int i = 0; i < M; i++) {
                if (b[i] == 'M') {
                    //找1人桌的桌子，假如不为空
                    if (!queueList.get(1).isEmpty()) {
                        //找到1人桌的桌子，输出桌号并出堆
                        x = queueList.get(1).poll();
                        writer.write(String.valueOf(x));
                        writer.newLine();
                    } else {
                        //找到0人桌的桌子，输出桌号并出堆
                        x = queueList.get(0).poll();
                        //将出堆的0人桌插入到1人桌的队列
                        queueList.get(1).offer(x);
                        writer.write(String.valueOf(x));
                        writer.newLine();
                    }
                } else {
                    if (!queueList.get(0).isEmpty()) {
                        x = queueList.get(0).poll();
                        queueList.get(1).offer(x);
                        writer.write(String.valueOf(x));
                        writer.newLine();
                    } else {
                        x = queueList.get(1).poll();
                        writer.write(String.valueOf(x));
                        writer.newLine();
                    }
                }
            }
            writer.flush();
        }
    }
}
